package com.leo;

import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AnimationDemo
 * @Description 跑马灯效果
 * @Author wangss
 * @date 2020.10.27 20:09
 * @Version 1.0
 */
public class AnimationDemo2 extends Application {

    private ImageView rightButton;

    private ImageView leftButton;

    private double image_y = 0;

    private double index_1 = 0;
    private double index_2 = 0;
    private double index_3 = 0;

    private double image_z = 0;

    private List<ImageView> list = new ArrayList<>();

    private Duration time = Duration.seconds(0.5);

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane pane = getPane(700, 300);

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(pane);

        AnchorPane.setTopAnchor(pane, 200d);
        AnchorPane.setLeftAnchor(pane, 400d);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1500);
        primaryStage.setHeight(900);
        primaryStage.centerOnScreen();
        primaryStage.setTitle("轮播图");
        primaryStage.show();

        leftButton.setOnMouseClicked(event -> {
            right_to_left();
        });
        rightButton.setOnMouseClicked(event -> {
            left_to_right();
        });

    }

    public Pane getPane(int width, int height) {

        leftButton = new ImageView("images/left.png");
        leftButton.setPickOnBounds(true);
        rightButton = new ImageView("images/left.png");
        rightButton.setRotate(180);
        // 这个作用是 true 整个图片可以选择，false 只能选择展示的部分
        rightButton.setPickOnBounds(true);

        leftButton.setPreserveRatio(true);
        rightButton.setPreserveRatio(true);

        HBox hBox = new HBox(width - leftButton.prefWidth(-1) * 2);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(leftButton, rightButton);

        ImageView iv1 = new ImageView("images/1.jpg");
        ImageView iv2 = new ImageView("images/2.jpg");
        ImageView iv3 = new ImageView("images/3.jpg");

        iv1.setPreserveRatio(true);
        iv2.setPreserveRatio(true);
        iv3.setPreserveRatio(true);

        iv1.setFitWidth(width / 1.3);
        iv2.setFitWidth(width / 1.3);
        iv3.setFitWidth(width / 1.3);

        image_z = 60;

        index_1 = 0 - image_z;
        index_2 = width / 2 - iv2.getFitWidth() / 2;
        index_3 = width - iv3.getFitWidth() + image_z;

        iv1.setTranslateX(index_1);
        iv2.setTranslateX(index_2);
        iv3.setTranslateX(index_3);

        image_y = height / 2 - iv1.prefHeight(-1) / 2;
        iv1.setTranslateY(image_y);
        iv2.setTranslateY(image_y);
        iv3.setTranslateY(image_y);

        iv1.setTranslateZ(image_z);
        iv2.setTranslateZ(0);
        iv3.setTranslateZ(image_z);

        list.add(iv1);
        list.add(iv2);
        list.add(iv3);

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.getChildren().addAll(iv1, iv2, iv3);

        SubScene subScene = new SubScene(anchorPane, width, height, true, SceneAntialiasing.BALANCED);
        PerspectiveCamera camera = new PerspectiveCamera();
        subScene.setCamera(camera);

        StackPane sp = new StackPane();
//        sp.setStyle("-fx-background-color: #FFB6c1");
        sp.getChildren().addAll(subScene, hBox);
        return sp;
    }

    public void right_to_left() {
        ImageView iv1 = list.get(0);
        ImageView iv2 = list.get(1);
        ImageView iv3 = list.get(2);
        left_to_middle_animation(iv1);
        middle_to_right_animation(iv2);
        right_to_left_animation(iv3);
        list.clear();
        list.add(iv3);
        list.add(iv1);
        list.add(iv2);
    }

    public void left_to_middle_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setNode(view);

        tt.setFromX(index_1);
        tt.setFromZ(image_z);

        tt.setToX(index_2);
        tt.setToZ(0);

        tt.play();
    }

    public void middle_to_right_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setFromX(index_2);
        tt.setFromZ(0);
        tt.setToX(index_3);
        tt.setToZ(image_z);

        FadeTransition ft1 = new FadeTransition(Duration.seconds(0.25));
        ft1.setFromValue(1);
        ft1.setToValue(0.5);

        FadeTransition ft2 = new FadeTransition(Duration.seconds(0.25));
        ft2.setFromValue(0.5);
        ft2.setToValue(1);

        SequentialTransition st = new SequentialTransition();
        st.getChildren().addAll(ft1, ft2);

        ParallelTransition pt = new ParallelTransition();
        pt.setNode(view);
        pt.getChildren().addAll(tt, st);

        pt.play();
    }

    public void right_to_left_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setNode(view);

        tt.setFromX(index_3);

        tt.setToX(index_1);

        tt.play();
    }

    public void left_to_right() {
        ImageView iv1 = list.get(0);
        ImageView iv2 = list.get(1);
        ImageView iv3 = list.get(2);
        right_to_middle_animation(iv3);
        middle_to_left_animation(iv2);
        left_to_right_animation(iv1);
        list.clear();
        list.add(iv2);
        list.add(iv3);
        list.add(iv1);
    }

    public void right_to_middle_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setNode(view);

        tt.setFromX(index_3);
        tt.setFromZ(image_z);

        tt.setToX(index_2);
        tt.setToZ(0);

        tt.play();
    }

    public void middle_to_left_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setFromX(index_2);
        tt.setFromZ(0);
        tt.setToX(index_1);
        tt.setToZ(image_z);

        FadeTransition ft1 = new FadeTransition(Duration.seconds(0.25));
        ft1.setFromValue(1);
        ft1.setToValue(0.5);

        FadeTransition ft2 = new FadeTransition(Duration.seconds(0.25));
        ft2.setFromValue(0.5);
        ft2.setToValue(1);

        SequentialTransition st = new SequentialTransition();
        st.getChildren().addAll(ft1, ft2);

        ParallelTransition pt = new ParallelTransition();
        pt.setNode(view);
        pt.getChildren().addAll(tt, st);

        pt.play();
    }

    public void left_to_right_animation(ImageView view) {
        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(time);
        tt.setNode(view);

        tt.setFromX(index_1);
        tt.setToX(index_3);

        tt.play();
    }
}
